#include<bits/stdc++.h>
using namespace std;
int main(){
  freopen("arena.in","r",stdin);
  freopen("arena.out","w",stdout);
  int p2[31];
  p2[0]=1;
  for(int i=1;i<=30;i++)
    p2[i]=p2[i-1]*2;
  int n,m;
  cin>>n>>m;
  int aa[n+1],c[m+1],a[n+1];
  for(int i=1;i<=n;i++)
    cin>>aa[i];
  for(int i=1;i<=m;i++)
    cin>>c[i];
  int K,o=-1;
  while(p2[++o]<n)
    K=p2[o];
  char d[o+1][p2[o]];
  for(int i=o;i>=1;i--)
    for(int j=1;j<=p2[i-1];j++)
      cin>>d[i][j];
  int t;
  cin>>t;
  while(t--){
    int x[4],ans=1;
    for(int i=1;i<=4;i++)
      cin>>x[i];
    for(int i=1;i<=n;i++)
      a[i]=(aa[i]^x[i%4]);
    for(int i=1;i<=m;i++){
      vector<int>v[2];
      for(int i=1;i<=c[i];i++)
        v[0].push_back(0),v[1].push_back(0);
      for(int i=1;i<=n;i++)
        v[0][i]=i;
      int R=1,rs=c[i],now=0;
      while(rs>1){
        for(int i=1;i<=rs;i+=2){
          if(d[R][i/2+1]==0){
            if(d[now][i]<d[now][i+1]){
              if(d[now][i]>R)
                v[abs(now-1)][i/2+1]=i;
              else
                v[abs(now-1)][i/2+1]=i+1;
            }
          }
          else{
            if(d[now][i]>d[now][i+1]){
              if(d[now][i]>R)
                v[abs(now-1)][i/2+1]=i;
              else
                v[abs(now-1)][i/2+1]=i+1;
            }
          }
        }
        R++;
        rs/=2;
      }
      ans=ans^(i*v[now][1]);
    }
    cout<<ans<<endl;
  }
  fclose(stdin);
  fclose(stdout);
  return 0;
}
